"""
title:修改用户资料
auothr:陈诚
date:2022/1/9
"""
from Common.Base import Base, open_browser
from Page.LoginPage import LoginPage

login_url = "http://localhost:8080/ecshop/user.php"    # 准备网址


class UpdateUserMessage(Base):
    """封装表现层,制作定位器"""
    center_loc = ('link text', '用户中心')               # 定位用户中心
    usermessage_loc = ('link text', '用户信息')          # 定位用户信息
    sercet_loc = ('css selector', 'input[value="0"]')  # 定位保密按钮
    boy_loc = ('css selector', 'input[value="1"]')     # 定位男按钮
    girl_loc = ('css selector', 'input[value="2"]')    # 定位女按钮
    email_loc = ('name', 'email')                      # 定位邮箱输入框
    QQ_loc = ('name', 'extend_field2')                 # 定位QQ输入框
    job_phone_loc = ('name', 'extend_field3')          # 定位办公电话输入框
    home_phone_loc = ('name', 'extend_field4')         # 定位家庭电话输入框
    phone_loc = ('name', 'extend_field5')              # 定位电话输入框
    question_loc = ('name', 'sel_question')            # 定位密码提示问题文本框
    answer_loc = ('name', 'passwd_answer')             # 定位答案输入框
    submit_loc = ('name', 'submit')                    # 定位确认修改按钮
    province = ('css selector', '#selProvinces_0 > option:nth-child(4)')  # 定位修改后的地址

    """封装操作层,操作元素"""
    def user_button(self):
        """点击用户中心"""
        self.click(self.center_loc)

    def user_message_button(self):
        """点击用户信息"""
        self.click(self.usermessage_loc)

    def sercet_button(self):
        """点击保密按钮"""
        self.click(self.sercet_loc)

    def boy_button(self):
        """点击男按钮"""
        self.click(self.boy_loc)

    def girl_button(self):
        """点击女按钮"""
        self.click(self.girl_loc)

    def input_eamil(self, email: str):
        """输入邮箱"""
        self.send_keys(self.email_loc, email)

    def input_QQ_number(self, QQ: str):
        """输入QQ号码"""
        self.send_keys(self.QQ_loc, QQ)

    def input_job_phone(self, job_phone: str):
        """输入工作号码"""
        self.send_keys(self.job_phone_loc, job_phone)

    def input_home_phone(self, home_phone: str):
        """输入家庭号码"""
        self.send_keys(self.home_phone_loc, home_phone)

    def input_phone(self, phone: str):
        """输入号码"""
        self.send_keys(self.phone_loc, phone)

    def question_text(self, question):
        """选择问题"""
        self.drop_down_list(self.question_loc, question)

    def input_answer(self, answer):
        """输入答案"""
        self.send_keys(self.answer_loc, answer)

    def sumit_button(self):
        """点击确认修改"""
        self.click(self.submit_loc)

    def is_update_success(self, province):
        """判断是否修改成功"""
        return self.is_text_equal_in_element(self.province, province)

    def gain_value(self, loc):
        """获取元素value值"""
        return self.find_element(loc).get_attribute('value')


if __name__ == '__main__':
    driver = open_browser()       # 打开浏览器
    login = LoginPage(driver)     # 实例化对象
    login.open_url(login_url)     # 输入网址
    login.input_username('root1') # 输入账号
    login.input_password(123456)  # 输入密码
    login.click_login()           # 点击登录

    update =UpdateUserMessage(driver)
    update.user_button()
    update.user_message_button()
    update.sercet_button()
    update.boy_button()
    update.girl_button()
    update.input_eamil('2923713823@qq.com')
    update.input_QQ_number('2923713823')
    update.input_job_phone('15228653560')
    update.input_home_phone('15228653560')
    update.input_phone('17738729278')
    update.question_text('我最喜欢的小说？')
    update.input_answer('霸道总裁爱上我')
    update.sumit_button()
